﻿// <copyright file="admin_Action_List.aspx.cs" company="NTalent">
// Copyright (c) 2010, NTalent, Inc. All rights reserved
// </copyright>
// <author>卢俊坤</author>
// <author>王彦博 符茂松</author>

using System;
using System.Linq;
using System.Web.UI.WebControls;
using Net.Ntalent.Action50.Core.Controls;
using Net.Ntalent.Action50.Core.Dao;
using Net.Ntalent.Action50.Core.Service;
using Net.Ntalent.Action50.Core.Util;

/// <summary>
/// 管理页面，显示所有action
/// </summary>
///<author>王彦博</author> 
/// <copyright>
/// Copyright (c) 2010, NTalent, Co. Ltd. All rights reserved.
/// </copyright>
/// <version>1.0.0</version>
public partial class admin_Action_List : PageBase
{
    /// <summary>
    /// 构造函数
    /// </summary>
    public admin_Action_List()
    {
        //设置权限，使未登录用户不能访问该页
        authentication = Constants.AUTHENTICATION_ADMIN; 
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName == "deleteAction")
        {
            //并不直接删除action，而是把欲删除的action的id存入hiddenField，等待弹出框确认之后再删除
            HiddenField_actionToBeDelete.Value = e.CommandArgument.ToString();
        }
    }

    /// <summary>
    /// LogOut
    /// </summary>
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        AuthenticationService authenticationService = new AuthenticationService();
        authenticationService.LogOut();
        Response.Redirect(Request.UrlReferrer.ToString());
    }

    /// <summary>
    /// 跳转到创建action的页面
    /// </summary>
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        Response.Redirect("admin_CreateNewAction.aspx");
    }

    protected void ListView1_DataBound(object sender, EventArgs e)
    {
        //如果数据不够一页，则不显示分页控件。
        long actionId = Convert.ToInt64(Request.QueryString["actionid"]);
        var actionDao = new ActionDao();
        long actionCount = actionDao.RetrieveAll(0, 0, "id").Count();
        if (actionCount <= 15)
        {
            DataPager1.Visible = false;
        }    
    }

    /// <summary>
    /// 删除某个action
    /// </summary>
    protected void DeleteAction(object sender, EventArgs e)
    {
        //点击删除之后，用js将欲删除的项的id写入一个HiddenField，弹出提示框.
        //点击确认后调用该方法，从HiddenField中读取欲删除的项的ID
        EditService es = new EditService();
        long actionId = Convert.ToInt64(HiddenField_actionToBeDelete.Value);
        es.DeleteAction(actionId);         
        ListView1.DataBind();  
    }

    /// <summary>
    /// 将action按照id降序排列
    /// </summary>
    protected void btn_sortdown_Click(object sender, EventArgs e)
    {      
        ObjectDataSource1.SelectParameters["orderBy"].DefaultValue = "id DESC";
        ListView1.DataBind();
    }

    /// <summary>
    /// 将action按照id升序排列
    /// </summary>
    protected void btn_sortup_Click(object sender, EventArgs e)
    {        
        ObjectDataSource1.SelectParameters["orderBy"].DefaultValue = "id";
        ListView1.DataBind();
    }
}
